define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: () => {
            const { reactive, onMounted, ref } = Vue
            const index = {
                setup() {
                    const state = reactive({
                        data: [],
                        filter: {
                            drawer: false,
                            data: {
                                user: { field: 'user_id', value: '' },
                                year: new Date().getFullYear()+'',
                                month: '',
                            },
                            tools: {
                                user: {
                                    type: 'tinputprepend',
                                    label: '用户',
                                    placeholder: '请输入查询内容',
                                    value: {
                                        field: 'user_id',
                                        value: '',
                                    },
                                    options: {
                                        data: [{
                                            label: '用户ID',
                                            value: 'user_id',
                                        },
                                            {
                                                label: '用户昵称',
                                                value: 'user.nickname',
                                            },
                                            {
                                                label: '用户手机号',
                                                value: 'user.mobile',
                                            }],
                                    }
                                },
                                createtime: {
                                    type: 'tdatetimerange',
                                    label: '时间',
                                    value: [],
                                },
                            },
                            condition: {},
                        },
                        statusStyle: {
                            '-2': 'danger',
                            '-1': 'warning',
                            0: 'info',
                            1: 'success',
                        },
                    })

                    function getData() {
                        let tempSearch = JSON.parse(JSON.stringify(state.filter.data));
                        let search = composeFilter(tempSearch, {
                            'user.nickname': 'like',
                            'user.mobile': 'like',
                            'createtime': 'range',
                        });
                        Fast.api.ajax({
                            url: 'shopro/commission/market',
                            type: 'GET',
                            data: {
                                page: pagination.page,
                                list_rows: pagination.list_rows,
                                ...search,
                            },
                        }, function (ret, res) {
                            state.data = res.data.data
                            pagination.total = res.data.total
                            return false
                        }, function (ret, res) { })
                    }

                    function onOpenFilter() {
                        state.filter.drawer = true
                    }
                    function onChangeFilter() {
                        pagination.page = 1
                        getData()
                        state.filter.drawer && (state.filter.drawer = false)
                    }

                    const pagination = reactive({
                        page: 1,
                        list_rows: 10,
                        total: 0,
                    })

                    function onDetail(id) {
                        Fast.api.open(`shopro/commission/market/detail/id/${id}?id=${id}`, "详情", {
                            callback() {
                                getData()
                            }
                        })
                    }

                    const exportLoading = ref(false);
                    function onExport(type) {
                        exportLoading.value = true;
                        let tempSearch = JSON.parse(JSON.stringify(state.filter.data));
                        let search = composeFilter(tempSearch, {
                            'user.nickname': 'like',
                            'user.mobile': 'like',
                            'createtime': 'range',
                        });

                        if (Config.save_type == 'download') {
                            window.location.href = `${Config.moduleurl}/shopro/commission/market/${type}?page=${pagination.page}&list_rows=${pagination.list_rows}&search=${search.search}`;
                            exportLoading.value = false;
                        } else if (Config.save_type == 'save') {
                            Fast.api.ajax({
                                url: `shopro/commission/market/${type}`,
                                type: 'GET',
                                data: {
                                    page: pagination.page,
                                    list_rows: pagination.list_rows,
                                    ...search,
                                },
                            }, function (ret, res) {
                                exportLoading.value = false;
                            }, function (ret, res) { })
                        }
                    }

                    onMounted(() => {
                        getData()
                    })
                    function onChangeGrant(id) {
                        Fast.api.open(`shopro/commission/market/grant_set?id=${id}`, "发放月度奖金", {
                            callback() {
                                getData()
                            }
                        })
                    }
                    return {
                        state,
                        getData,
                        onOpenFilter,
                        onChangeFilter,
                        pagination,
                        exportLoading,
                        onExport,
                        onChangeGrant,
                        onDetail,
                    }
                }
            }
            createApp('index', index);
        },
        detail: () => {
            const { reactive, onMounted } = Vue
            const detail = {
                setup() {

                    const state = reactive({
                        id: new URLSearchParams(location.search).get('id'),
                        data: {},
                    })

                    function getDetail() {
                        Fast.api.ajax({
                            url: `shopro/commission/market/detail/id/${state.id}`,
                            type: 'GET',
                        }, function (ret, res) {
                            state.data = res.data
                            console.log(state.data)
                            return false
                        }, function (ret, res) { })
                    }

                    function grant_set() {
                        Fast.api.open(`shopro/commission/market/grant_set?level=${state.data.level}`, "发放月度奖金", {
                            callback() {
                                getDetail()
                            }
                        })
                    }
                    onMounted(() => {
                        getDetail()
                    })

                    function onChangeGrant(id) {
                        Fast.api.open(`shopro/commission/market/grant_set?id=${id}`, "发放月度奖金", {
                            callback() {
                                getDetail()
                            }
                        })
                    }
                    return {
                        state,
                        getDetail,
                        grant_set,
                        onChangeGrant
                    }
                }
            }
            createApp('detail', detail);
        },
        grant_set: () => {
            Controller.form();
        },
        form: () => {
            const { reactive, onMounted, getCurrentInstance } = Vue
            const { ElMessageBox } = ElementPlus
            const grant_set = {
                setup() {
                    const { proxy } = getCurrentInstance();

                    const state = reactive({
                        id: new URLSearchParams(location.search).get('id')
                    })
                    console.log(state)
                    console.log(new URLSearchParams(location.search))

                    const form = reactive({
                        model: {
                            remark: '',
                            voucher: '',
                        },
                        rules: {},
                    })

                    function onConfirm() {
                        ElMessageBox.confirm('确认是否发放奖金', '提示', {
                            confirmButtonText: '确定',
                            cancelButtonText: '取消',
                            type: 'warning',
                        }).then(() => {
                            onChange()
                        });

                        function onChange() {
                            Fast.api.ajax({
                                url: `shopro/commission/market/edit/id/`+state.id,
                                type: 'POST',
                                data: form.model,
                            }, function (ret, res) {
                                Fast.api.close()
                            }, function (ret, res) { })
                        }
                    }

                    return {
                        form,
                        onConfirm
                    }
                }
            }
            createApp('grant_set', grant_set);
        }
    };
    return Controller;
});
